package cn.edu.neusoft.food229.model;

import java.util.List;

import cn.edu.neusoft.food229.bean.Comments;
import cn.edu.neusoft.food229.bean.FoodBean;
import cn.edu.neusoft.food229.iface.Commentsiface;
import cn.edu.neusoft.food229.listener.CommentsListener;
import cn.edu.neusoft.food229.listener.FoodDetailListener;
import cn.edu.neusoft.food229.service.FoodService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static cn.edu.neusoft.food229.Commen.Commen.BASEURL;

public class CommentsModel implements Commentsiface{
    private Retrofit retrofit;
    public CommentsModel(){
        //使用Retrofit--1
        retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    //菜单详情
    @Override
    public void getResultFoodList(final String food_id, final FoodDetailListener listener) {
                FoodService service=retrofit.create(FoodService.class);
        Call<FoodBean> call=service.getFoodid(food_id);
        call.enqueue(new Callback<FoodBean>() {
            @Override
            public void onResponse(Call<FoodBean> call, Response<FoodBean> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    listener.onSuccess(response.body(),food_id);
                    System.out.println("---"+response.body().toString() );
                }else listener.onFail("on response fail");
            }

            @Override
            public void onFailure(Call<FoodBean> call, Throwable t) {
                listener.onFail(t.toString());

            }
        });
    }
// --------------评论列表
    @Override
    public void getResult(final String food_id, final CommentsListener listener) {
        FoodService service
                =retrofit.create(FoodService.class);
        Call<List<Comments>> call =service.getComments(food_id);
        call.enqueue(new Callback<List<Comments>>() {
            @Override
            public void onResponse(Call<List<Comments>> call, Response<List<Comments>> response) {
                if(response.isSuccessful() && response!=null){
                    listener.onResponce((List<Comments>)response.body(),"1");
                }
                else
                    listener.onFail("on response fail");
            }

            @Override
            public void onFailure(Call<List<Comments>> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }


}
